

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.build_train_network &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.run_check" href="mindspore.run_check.html" />
    <link rel="prev" title="mindspore.get_log_config" href="mindspore.get_log_config.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="../mindspore.html">mindspore</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id1">张量</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id2">参数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id3">数据类型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id4">随机种子</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id5">模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id6">数据处理工具</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id7">混合精度管理</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id8">序列化</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id9">即时编译</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id10">日志</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../mindspore.html#id11">自动混合精度</a><ul class="current">
<li class="toctree-l3 current"><a class="current reference internal" href="#">mindspore.build_train_network</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id12">安装验证</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id13">调试</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.html#id14">内存回收</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.html">mindspore</a> &raquo;</li>
        
      <li>mindspore.build_train_network</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/mindspore/mindspore.build_train_network.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-build-train-network">
<h1>mindspore.build_train_network<a class="headerlink" href="#mindspore-build-train-network" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.build_train_network">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.</code><code class="sig-name descname">build_train_network</code><span class="sig-paren">(</span><em class="sig-param">network</em>, <em class="sig-param">optimizer</em>, <em class="sig-param">loss_fn=None</em>, <em class="sig-param">level='O0'</em>, <em class="sig-param">boost_level='O0'</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.build_train_network" title="Permalink to this definition">¶</a></dt>
<dd><p>构建混合精度训练网络。</p>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>network</strong> (Cell) – 定义网络结构。</p></li>
<li><p><strong>optimizer</strong> (Optimizer) – 定义优化器，用于更新权重参数。</p></li>
<li><p><strong>loss_fn</strong> (Union[None, Cell]) – 定义损失函数。如果为None， <cite>network</cite> 中应该包含损失函数。默认值：None。</p></li>
<li><p><strong>level</strong> (str) – 支持[“O0”, “O2”, “O3”, “auto”]。默认值：”O0”。</p>
<ul>
<li><p><strong>“O0”</strong> - 不变化。</p></li>
<li><p><strong>“O2”</strong> - 将网络精度转为float16， <cite>BatchNorm</cite> 和 <cite>loss_fn</cite> 保持float32精度，使用动态调整损失缩放系数（loss scale）的策略。</p></li>
<li><p><strong>“O3”</strong> - 将网络精度转为float16，不使用损失缩放策略，并设置 <cite>keep_batchnorm_fp32</cite> 为False。</p></li>
<li><p><strong>auto</strong> - 为不同处理器设置专家推荐的混合精度等级，如在GPU上设为”O2”，在Ascend上设为”O3”。该设置方式可能在部分场景下不适用，建议用户根据具体的网络模型自定义设置 <cite>amp_level</cite> 。 <cite>keep_batchnorm_fp32</cite> ， <cite>cast_model_type</cite> 和 <cite>loss_scale_manager</cite> 属性由level自动决定。</p></li>
</ul>
</li>
<li><p><strong>boost_level</strong> (str) – <cite>mindspore.boost</cite> 中参数 <cite>level</cite> 的选项，设置boost的训练模式级别。支持[“O0”, “O1”, “O2”]。默认值: “O0”。</p>
<ul>
<li><p><strong>“O0”</strong> - 不变化。</p></li>
<li><p><strong>“O1”</strong> - 开启boost模式，性能提升20%左右，准确率与原始准确率相同。</p></li>
<li><p><strong>“O2”</strong> - 开启boost模式，性能提升30%左右，准确率降低小于3%。如果设置了”O1”或”O2”模式，boost相关库将自动生效。</p></li>
</ul>
</li>
<li><p><strong>cast_model_type</strong> (mindspore.dtype) – 支持float16，float32。如果设置了该参数，网络将被转化为设置的数据类型，而不会根据设置的level进行转换。</p></li>
<li><p><strong>keep_batchnorm_fp32</strong> (bool) – 当网络被设置为float16时，配置为True，则BatchNorm将保持在float32下运行。设置level不会影响该属性。</p></li>
<li><p><strong>loss_scale_manager</strong> (Union[None, LossScaleManager]) – 如果不为None，必须是 <a class="reference internal" href="mindspore.LossScaleManager.html#mindspore.LossScaleManager" title="mindspore.LossScaleManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.LossScaleManager</span></code></a> 的子类，用于缩放损失系数(loss scale)。设置level不会影响该属性。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>ValueError</strong> - 在CPU上，属性 <cite>loss_scale_manager</cite> 不是 <cite>None</cite> 或 <cite>FixedLossScaleManager</cite> （其属性 <cite>drop_overflow_update=False</cite> ）。</p></li>
</ul>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.run_check.html" class="btn btn-neutral float-right" title="mindspore.run_check" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.get_log_config.html" class="btn btn-neutral float-left" title="mindspore.get_log_config" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>